package br.com.massai.controller.interceptor;

import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import br.com.massai.controller.annotation.Login;
import br.com.massai.controller.filter.ControleAcesso;

@Interceptor @Login
public class LoginInterceptor implements Serializable {

	private static final long serialVersionUID = -5564669940373812231L;

	@Inject private ControleAcesso session;

	@AroundInvoke
	public Object verifySession(InvocationContext ic){
		try {
			FacesContext ctx = FacesContext.getCurrentInstance();
			if(!session.loggedIn())
				ctx.getApplication().getNavigationHandler().handleNavigation(ctx, null, "login");
				return ic.proceed();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
